Namespace My

    ' The following events are availble for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            AppDomain.CurrentDomain.SetData("DataDirectory", IO.Path.Combine(My.Application.Info.DirectoryPath, "AppData"))
            If My.Settings.AppVersion Is Nothing Then 'No AppVersion info in current settings
                If My.Settings.GetPreviousVersion("AppVersion") IsNot Nothing Then 'There is prevous version of settings
                    If MsgBox(String.Format(My.Resources.NewVersionOfApplicationInstalled, My.Application.Info.Title), MsgBoxStyle.YesNo Or MsgBoxStyle.Question, My.Application.Info.Title) = MsgBoxResult.Yes Then
                        'User wants to upgrade settings
                        My.Settings.Upgrade()
                        My.Settings.AppVersion = My.Application.Info.Version
                    Else 'User don't want to upgrade settings - do same steps as on first run
                        My.Settings.AppVersion = My.Application.Info.Version
                        If Not FirstRun() Then My.Settings.AppVersion = Nothing : Process.GetCurrentProcess.Kill()
                    End If
                Else 'There are no application settings - seems like first run after install
                    My.Settings.AppVersion = My.Application.Info.Version
                    If Not FirstRun() Then My.Settings.AppVersion = Nothing : Process.GetCurrentProcess.Kill()
                End If
            ElseIf My.Settings.AppVersion < My.Application.Info.Version Then 'This shouldn't happen (maybe)
                My.Settings.AppVersion = My.Application.Info.Version
            End If
            If My.Settings.UICulture <> "" Then
                Try
                    Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo(My.Settings.UICulture)
                Catch
                    My.Settings.UICulture = ""
                End Try
            End If
            My.Settings.Save()
        End Sub
        ''' <summary>Runs a  setup wizard</summary>
        Function FirstRun() As Boolean
            Return New frmFirstRun().ShowDialog = DialogResult.OK
        End Function
    End Class
End Namespace

